layui.config({
    base: '/html/js/'
}).use([
    'jquery',
    'form',
    'element',
    'layer',
    'laytpl',
    'fun'
],function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var fun = layui.fun;

    // 监听窗口大小
    if( $(window).width() <= 800 ){
        $('.layui-nav').addClass('layui-nav-tree');
    }else{
        $('.layui-nav').removeClass('layui-nav-tree');
    }
    $(window).on('resize',function () {
        var width = $(this).width();
        if( width <= 800 ){
            $('.layui-nav').addClass('layui-nav-tree');
        }else{
            $('.layui-nav').removeClass('layui-nav-tree');
        }
    });

    // 监听a连接点击
    $(document).on('click', 'a', function () {

        var thisObj = $(this);
        var url = thisObj.attr('href');

        // 防止多次点击
        if( thisObj.hasClass('click') ){

            return false;
        }

        thisObj.addClass('click');

        // 验证url是否符合规定
        if( url == 'javascript:;' || url == undefined || url == "#" || url == "" ){

            return false;
        }else if(url.indexOf("http://") >= 0 || url.indexOf("https://") >= 0){

            window.open(url);
            return false;
        }else if( url.indexOf('#') < 0 ){

            return true;
        }

        // 获取连接并设置内容
        fun.getUrl(url.replace('#', ''));

        // 设置当前地址为上一页面地址
        Cookies.set('back-url', '#'+layui.router().href);

        // 设置浏览器地址栏
        window.location.replace(url);

        $(window).scrollTop(0);

        thisObj.removeClass('click');

        return false;
    });

    // 返回上一级点击
    $(document).on('click', '.kt-back', function () {

        var backUrl = Cookies.get('back-url');

        if( backUrl != undefined ){

            // 获取内容
            fun.getUrl(backUrl.replace('#', ''));

            // 设置浏览器地址栏
            window.location.replace(backUrl);
        }

        return false;
    });

    // 滚动导航固定
    $(window).scroll(function () {
        if( $(window).width() > 800 ){

            if( $(window).scrollTop() > 105){

                $('.kt-top').css({
                    'position':'fixed',
                    'top':0, 'left':0,
                    'right':0,
                    'background-color':'#fff'
                });
                $('.kt-welcome').css({'margin-top':'-100px','transition':'none'});
            }else if ( $(window).scrollTop() == 0 ){

                $('.kt-top').css({
                    'position':'unset',
                    'top':0, 'left':0,
                    'right':0,
                    'background-color':'#fff'
                });
                $('.kt-welcome').css({'margin-top':'0','transition':'all .5s'});
            }
        }

    });

    // 检测路由
    var route = layui.router().href;
    // 获取路由设置内容
    var routeParam = '/html/views/index.html';

    if( route !== undefined){
        var routeStart = route.indexOf('/');
        if( routeStart !== undefined && routeStart >= 0 ){

            // 路由参数
            routeParam = route.substring(routeStart,routeStart.length);
        }
    }

    // 获取路由内容
    fun.getUrl(routeParam);

    /*注册按钮点击*/
    $(document).on('click','.kt-register-btn',function(){

        fun.registerBox.show();
    });

    /*登陆按钮点击*/
    $(document).on('click','.kt-login-btn',function(){

        fun.loginBox.show();
    });
    
    // 登录注册框关闭
    $(document).on('click', '.kt-close', function () {
        layer.closeAll();
    });
    
    /*登录注册切换*/
    $(document).on('click','.kt-login-switch',function(){

        fun.loginSwitch($(this));
    });

    /*登陆注册表单表单提交*/
    $(document).on('submit','.kt-form',function(){

        fun.loginSubmit($(this));

        return false;
    });

    /*键盘按下存储密码*/
    $(document).on("keyup",'input[type="password"]',function() {
        var val = $(this).val();
        $(this).attr('kt-password',val);
    });

    /*显示密码*/
    $(document).on("click",'.showPassword',function() {
        var thisObj = $(this).siblings('input');

        if(thisObj.attr('type') == 'text') {

            thisObj.attr('type', 'password');

            $(this).addClass('icon-zhengyan');
            $(this).removeClass('icon-biyan');
        }else{

            thisObj.attr('type', 'text');
            thisObj.attr('kt-password');

            $(this).removeClass('icon-zhengyan');
            $(this).addClass('icon-biyan');
        }
    });

    /*按下退出登录*/
    $(document).on('click','.kt-header-userinfo .kt-loginout',function(e){

        loginOutConfirm = layer.confirm('您确定要退出当前账号吗？', {
            title: '<span class="iconfont icon-dengpao" style="font-size:20px;"></span> <span>悄悄话</span>',
            btn: ['退出','取消'], //按钮
            closeBtn: 0,
            skin: 'kt-confirm'
        }, function(){
            $.ajax({
                url: '/index/do_member/loginout.html',
                success: function(data){
                    if(data.code == 0){
                        layer.msg('您已退出！');

                        $('header .nav-icons').html('<li><a href="javascript:;" class="kt-register-btn"><i class="ion-person-add"></i><div>免费注册</div></a></li><li><a href="javascript:;" class="kt-login-btn"><i class="ion-person"></i><div>立即登录</div></a></li>');

                        layer.close(loginOutConfirm);
                    }
                }
            });
        });
    });

    // 发布按钮动画效果
    $('.kt-releaseArticle').addClass('kt-releaseArticle-animation-before');

    // 发布按钮点击
    $(document).on('click', '.kt-releaseArticle',function (e) {
        e.stopPropagation();
    });
    $(document).on('animationend','.kt-releaseArticle-animation-before',function(){
        $(this).addClass('kt-releaseArticle-animation-after');
        $(this).removeClass('kt-releaseArticle-animation-before');
    });
    $(document).on('animationend','.kt-releaseArticle-animation-after',function(){
        $(this).addClass('kt-releaseArticle-animation-before');
        $(this).removeClass('kt-releaseArticle-animation-after');
    });

    // 手机端导航点击显示搜索
    $(document).on('click', '.kt-nav', function () {

        if( $(window).width() <= 800 ) {
            if( !$('.kt-welcome').hasClass('click') ){

                $('.kt-welcome').addClass('click');

                // 获取模板HTML
                fun.getTpl('/html/tpl/search-top.tpl',function (searchhistroy) {

                    var html = '<div class="kt-searchBg">' + searchhistroy + '</div>';
                    $('.kt-welcome').append(html);

                    $('.kt-body').css('filter', 'blur(15px)');
                    $('.footer').css('filter', 'blur(15px)');

                    // 禁止body滚动
                    $('html').addClass('not-scroll');
                });
            }
        }
    });

    // 手机端点击显示导航
    $(document).on('click', '.kt-nav .nav-ctrl', function (e) {

        // 阻止父级冒泡
        e.stopPropagation();

        if( $(this).hasClass('click') ){

            $(this).removeClass('click');
            $(this).siblings('.layui-nav-tree').removeClass('show');
        }else{

            $(this).addClass('click');
            $(this).siblings('.layui-nav-tree').addClass('show');
        }
    });

    // 点赞效果
    const shape = new mojs.Shape(fun.shape.shapeObj);
    const burst = new mojs.Burst(fun.burst.burstObj);
    $(document).on('click', '.love', function(){

        var thisObj = $(this);

        // 检测是否登录
        // if(!isLogin()){
        //     return false;
        // }

        var checked = thisObj.attr('data-checked');

        var id = thisObj.attr('kt-article_id');
        var click = thisObj.children('div').html();

        if(checked == 'undefined' || checked != 'checked'){

            $(this).children('span').css({'color':'#00c1de','text-shadow':'0 0 1px #989898'});
            $(this).attr('data-checked','checked');

            thisObj.children('div').html(Number(click)+1);
            clickType = 'addition';// 增加还是减少
        }else{

            $(this).children('span').css({'color':'#989898','text-shadow':'unset'});
            $(this).removeAttr('data-checked');

            thisObj.children('div').html(Number(click)-1);

            clickType = 'subtraction';// 增加还是减少
        }

        $.ajax({
            url:'/index/do_article/doarticleedit.html',
            data:{'id':id,'clickType':clickType},
            type:'post',
            error:function(){
                if(clickType == 'subtraction'){

                    layer.msg('系统繁忙，取消赞失败了，抱歉哦！',{skin:'kt-error'});
                }else{

                    layer.msg('系统繁忙，点赞失败了，抱歉哦！',{skin:'kt-error'});
                }
            }
        });

        // 获取当前元素的offset减去当前元素的宽高  就是中心位置
        y = $(this).children('.iconfont').offset().top+($(this).children('.iconfont').height()/2);
        x = $(this).children('.iconfont').offset().left+($(this).children('.iconfont').width()/2);

        // 点赞
        if(clickType == 'addition'){

            // 定义圆动画对象
            fun.shape.shapeObj.x = x;	// x坐标
            fun.shape.shapeObj.y = y;	// y坐标
            fun.shape.shapeObj.stroke = { '#00c1de' : '#ccc' }; // 颜色渐变
            fun.shape.shapeObj.opacity = { 1 : 0 }; // 透明度渐变
            fun.shape.shapeObj.radius = { 0 : 50 }; // 大小渐变
            fun.shape.shapeObj.strokeWidth = { 0 : 500 }; // 大小渐变

            // 定义爆炸粒子动画对象
            fun.burst.burstObj.x = x;	// x坐标
            fun.burst.burstObj.y = y;	// y坐标
            fun.burst.burstObj.radius = { 0 : 50 }; // 大小渐变
            fun.burst.burstObj.angle = { 0 : 180 }; // 旋转方向渐变
            fun.burst.burstObj.scale = { 0 : 5 }; //
            fun.burst.burstObj.children.fill = { '#00c1de' : '#00c1de' }; // 颜色渐变

        }else {
            // 取消点赞

            // 定义圆动画对象
            fun.shape.shapeObj.x = x;	// x坐标
            fun.shape.shapeObj.y = y;	// y坐标
            fun.shape.shapeObj.stroke = {'#00c1de': '#ccc'}; // 颜色渐变
            fun.shape.shapeObj.opacity = {0: 1}; // 透明度渐变
            fun.shape.shapeObj.radius = {50: 0}; // 大小渐变
            fun.shape.shapeObj.strokeWidth = {500: 0}; // 边框大小渐变

            // 定义爆炸粒子动画对象
            fun.burst.burstObj.x = x;	// x坐标
            fun.burst.burstObj.y = y;	// y坐标
            fun.burst.burstObj.radius = {50: 0}; // 大小渐变
            fun.burst.burstObj.angle = {180: 0}; // 旋转方向渐变
            fun.burst.burstObj.scale = {5: 0}; //
            fun.burst.burstObj.children.fill = {'#00c1de': '#ccc'}; // 颜色渐
        }

        // 执行重定义
        shape.tune(fun.shape.shapeObj).replay();
        burst.tune(fun.burst.burstObj).replay();

    });

    /**
     * [label选择文件]
     */
    $(document).on('change','.layui-form input[type="file"]',function(){

        fun.chooseFile.show($(this),this.files[0]);

    });

    // 关闭图片
    $(document).on('click', '.kt-upload-show .icon-guanbi', function () {

        fun.chooseFile.hide($(this));
    });
});
